package com.codeying.servlet.userComment;

import com.codeying.servlet.AbsServlet;
import com.codeying.entity.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

import com.codeying.utils.*;
import com.codeying.utils.component.*;

/**
 * 保存和新增评论
 */
@WebServlet("/userComment/save")
public class SaveUserCommentServlet extends AbsServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        int res = 0;
        UserComment entityTemp = new UserComment();
        //将参数全部转化成Map格式
        Map<String, Object> reqMap = CommonUtils.requestToMap(req);
        String id = (String) reqMap.get("id");
        entityTemp.setId(Integer.parseInt(id));
        String userid = ""; //用户编号
        String username = ""; //用户名
        String rolech = ""; //用户角色
        String content = ""; //内容
        Date createtime = null; //发布时间
        String ctid = ""; //内容编号
        String type = ""; //内容类型
        String status = ""; //状态
        username = (String) reqMap.get("username");
        entityTemp.setUsername(username);//String类型参数
        rolech = (String) reqMap.get("rolech");
        entityTemp.setRolech(rolech);//String类型参数
        content = (String) reqMap.get("content");
        entityTemp.setContent(content);//String类型参数
        createtime = DateUtil.strToDate((String) reqMap.get("createtime"));
        entityTemp.setCreatetime(createtime);
        status = (String) reqMap.get("status");
        entityTemp.setStatus(status);//String类型参数

        //新增或更新
        if (entityTemp.getId() == 0 || "".equals(entityTemp.getId())) {//新增
            id = String.valueOf(CommonUtils.newId());
            entityTemp.setId(Integer.parseInt(id));
            createtime = new Date();
            entityTemp.setCreatetime(createtime);
            //如果有唯一字段，则在此校验 （字段不为空在前端校验）
            //=========添加之前

            //=========添加之前
            res = userCommentService.insert(entityTemp);
        } else {
            //=========修改之前

            //=========修改之前
            res = userCommentService.updateById(entityTemp);
        }

        if (res != 0) {
            resp.getWriter().print(ApiResult.success());//标记保存成功
        } else {
            resp.getWriter().print(ApiResult.fail("保存失败"));//保存失败
        }
    }
}

